package com.fwpsl.springframework.annotation.importA;

import com.fwpsl.springframework.annotation.importA.bean.RainBow;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;

/**
 * @author: 风戏fw
 * @date: 2024/5/21
 * @description:
 */
public class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {

    /*
     * AnnotationMetadata：当前类的注解信息
     * BeanDefinitionRegistry：BeanDefinition注册类
     * 把所有需要添加到容器中的bean，调用BeanDefinitionRegistry.registerBeanDefinition手动注入
     */
    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
        boolean definition = registry.containsBeanDefinition("com.fwpsl.springframework.annotation.importA.bean.Red");
        boolean definition2 = registry.containsBeanDefinition("com.fwpsl.springframework.annotation.importA.bean.Yellow");
        if (definition && definition2) {
            //指定bean定义信息（bean的类型，bean的scope）
            RootBeanDefinition rootBeanDefinition = new RootBeanDefinition(RainBow.class);
            //注册一个bean，指定bean名
            registry.registerBeanDefinition("rainBow", rootBeanDefinition);
        }
    }
}
